package dao.impl;

import dao.DownInfoDao;
import domian.DownInfo;
import utils.BaseDAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DownInfoDaoImpl implements DownInfoDao {
    @Override
    public boolean addDownInfo(DownInfo downInfo) {
        String sql = "insert into downinfo (uid,fid,down_date) values (?,?,?)";
        Object obj [] ={downInfo.getUid(),downInfo.getFid(),downInfo.getDown_date()};
        int i = BaseDAO.executeUpdate(sql, obj);
        return i>0;
    }

    @Override
    public String findUpDownDataByUidAndFid(Integer uid, Integer fid) {
        Connection connection =null;
        PreparedStatement preparedStatement =null;
        ResultSet resultSet = null;
        String sql = "select down_date from downinfo where uid=? and fid=?";
        try {
            String downDate =null;
            connection=BaseDAO.getConnection();
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setInt(1,uid);
            preparedStatement.setInt(2,fid);
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()){
                 downDate = resultSet.getString("down_date");
            }
           return downDate;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            BaseDAO.close(connection,preparedStatement,resultSet);
        }

    }
}
